//
//  WYRBaseiew.m
//  事件的产生和传递
//
//  Created by 伍银荣 on 2021/7/6.
//  Copyright © 2021 com.WYR. All rights reserved.
//

#import "WYRBaseiew.h"

@implementation WYRBaseiew

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"%@----touchesBegan",[self class]);
    [super touchesBegan:touches withEvent:event];
}

//-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
//    NSLog(@"%@----hitTest",[self class]);
//    return [super hitTest:point withEvent:event];
//}

//hitTest方法内部的参考实现
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    NSLog(@"%@----hitTest",[self class]);
    //如果控件不允许与用户交互,那么返回nil
    if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) {
        return nil;
    }
    
    //如果点击的这个点不在当前控件中,那么返回nil
    if (![self pointInside:point withEvent:event]) {
        return nil;
    }
    
    //从后向前遍历每一个子控件
    for (int i = (int)self.subviews.count - 1; i >= 0; i--) {
        //获取一个子控件
        UIView *lastView = self.subviews[i];
        //把当前触摸点坐标转换为相对于子控件的触摸点坐标  convert:改变,转变
        CGPoint subPoint = [self convertPoint:point toView:lastView];
        //判断是否在子控件中找到了更合适的子控件
        UIView *nextView = [lastView hitTest:subPoint withEvent:event];
        if (nextView) {
            return nextView;
        }
    }
    //以上都没有执行return,那么返回自己(表示子控件中没有更合适的了)
    return self;
}


//当前点击的view是否在这个点(point)上
//-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
//    NSLog(@"%@----pointInside",[self class]);
//    return [super pointInside:point withEvent:event];
//}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
